/**
 * 
 */
package com.clh.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clh.dao.PayTypeDao;
import com.clh.entity.PayType;
import com.clh.util.DBUtil;

/**
 * @Description: TODO
 * @author 陈志艺
 * @date 2022年5月31日 下午3:22:27
 * @version v1.0
 */
public class PayTypeDaoImpl implements PayTypeDao {

	@Override
	public List<PayType> find() {
		List<PayType> list = new ArrayList<PayType>();
		StringBuffer sql = new StringBuffer();
		sql.append("select").append("  type_id,").append("  type_name").append(" from pay_type");
		ResultSet rs = DBUtil.query(sql.toString());
		System.out.println(sql.toString());
		try {
			while (rs.next()) {
				Integer typeId = rs.getInt("type_id");
				String typeName = rs.getString("type_name");

				PayType payType = new PayType();
				payType.setTypeId(typeId);
				payType.setTypeName(typeName);
				list.add(payType);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<PayType> findBypage(String typeName, int pageNum, int pageSize) {
		List<PayType> list = new ArrayList<PayType>();
		StringBuffer sql = new StringBuffer();
		sql.append("select").append("  type_id,").append("  type_name").append(" from pay_type");
		if (true) {
			sql.append(" where 1=1");
		}
		
		// 根据参数情况下拼接不同的sql语句
		if (typeName != null && !"".equals(typeName)) {
			sql.append("  and type_name like '" + typeName + "'");
		}
		// 分页功能
		sql.append(" limit " + (pageNum - 1) * pageSize + "," + pageSize);// limit 0,2
		System.out.println(sql.toString());
		ResultSet rs = DBUtil.query(sql.toString());
		try {
			while (rs.next()) {
				Integer typeId = rs.getInt("type_id");
				typeName = rs.getString("type_name");

				PayType payType1 = new PayType();
				payType1.setTypeId(typeId);
				payType1.setTypeName(typeName);
				list.add(payType1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int countByConditon(String typeName) {
		int n = 0;
		StringBuilder sql = new StringBuilder();
		sql.append("select").append("  type_id,").append("  type_name").append(" from pay_type");
		// 动态SQL
		if (true) {
			sql.append("  where 1=1 ");
		}
		if (typeName != null && !"".equals(typeName)) {
			sql.append("  and type_name like '" + typeName + "'");
		}
		System.out.println(sql.toString());
		ResultSet rs = DBUtil.query(sql.toString());
		try {
			while (rs.next()) {
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public List<PayType> findByCondition(String typeName) {
		List<PayType> list = new ArrayList<PayType>();
		StringBuilder sql = new StringBuilder();
		sql.append("select").append("  type_id,").append("  type_name").append(" from pay_type");
		// 动态SQL
		if (true) {
			sql.append("  where 1=1 ");
		}
		if (typeName != null && !"".equals(typeName)) {
			sql.append("  and type_name like '" + typeName + "'");
		}
		System.out.println(sql.toString());
		ResultSet rs = DBUtil.query(sql.toString());
		try {
			while (rs.next()) {
				Integer typeId = rs.getInt("type_id");
				typeName = rs.getString("type_name");

				PayType payType1 = new PayType();
				payType1.setTypeId(typeId);
				payType1.setTypeName(typeName);
				list.add(payType1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public PayType findById(int id) {
		PayType payType = new PayType();
		StringBuilder sql = new StringBuilder();
		sql.append("select").append("  type_id,").append("  type_name").append(" from pay_type").append(" where type_id=?");
		System.out.println(sql.toString());
		ResultSet rs = DBUtil.query(sql.toString(), id);
		try {
			while (rs.next()) {
				Integer typeId = rs.getInt("type_id");
				String typeName = rs.getString("type_name");

				 payType = new PayType();
				payType.setTypeId(typeId);
				payType.setTypeName(typeName);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return payType;
	}

	@Override
	public int insert(PayType payType) {
		StringBuffer sql = new StringBuffer();
		sql.append("insert into pay_type(type_name)").append(" value(?)");
		int n=DBUtil.update(sql.toString(), payType.getTypeName());
		return n;
	}

	@Override
	public int update(PayType payType) {
		return DBUtil.update("update pay_type set type_name=? where type_id=?", payType.getTypeName(),payType.getTypeId());
	}

	@Override
	public int delete(int id) {
		int rs = DBUtil.update("DELETE FROM pay_type WHERE type_id=?", id);
		return rs;
	}

}
